package com.bjpowernode.workbench.web.controller;

import com.bjpowernode.commons.contants.Contants;
import com.bjpowernode.commons.domain.ReturnObject;
import com.bjpowernode.commons.utils.DateUtils;
import com.bjpowernode.settings.web.domain.User;
import com.bjpowernode.settings.web.service.impl.UserServiceImpl;
import com.bjpowernode.workbench.web.domain.*;
import com.bjpowernode.workbench.web.service.ActivityService;
import com.bjpowernode.workbench.web.service.impl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class WorkBenchTransactionController {

@Autowired
private TranServiceImpl tranService;

@Autowired
private DicValueServiceImpl dicValueService;

@Autowired
private UserServiceImpl userService;

@Autowired
private ActivityService activityService;

@Autowired
private ContactsServiceImpl contactsService;

@Autowired
private CustomerServiceImpl customerService;

@Autowired
private TranHistoryServiceImpl tranHistoryService;

@Autowired
private TranRemarkServiceImpl tranRemarkService;

    @RequestMapping("/workbench/transaction/index.do")
    public String index(HttpServletRequest request){
        List<DicVlaue> clueStateList = dicValueService.selectDicValueByTypeCode("clueState");
        List<DicVlaue> stageList = dicValueService.selectDicValueByTypeCode("stage");
        List<DicVlaue> returnStateList = dicValueService.selectDicValueByTypeCode("returnState");
        List<DicVlaue> sourceList = dicValueService.selectDicValueByTypeCode("source");
        List<DicVlaue> returnPriorityList = dicValueService.selectDicValueByTypeCode("returnPriority");
        List<DicVlaue> appellationList = dicValueService.selectDicValueByTypeCode("appellation");
        List<DicVlaue> transactionTypeList = dicValueService.selectDicValueByTypeCode("transactionType");

        request.setAttribute("clueStateList",clueStateList);
        request.setAttribute("stageList",stageList);
        request.setAttribute("returnPriorityList",returnPriorityList);
        request.setAttribute("returnStateList",returnStateList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("appellationList",appellationList);
        request.setAttribute("transactionTypeList",transactionTypeList);
        return "/workbench/transaction/index";
    }

    @ResponseBody
    @RequestMapping("/workbench/transaction/selectTranDetailForPageByCondition.do")
    public Object selectTranDetailForPageByCondition(Tran tran){
        int pageNo = (tran.getPageNo()-1)*(tran.getPageSize());
        tran.setPageNo(pageNo);
        List<Tran> tranList = tranService.selectTranDetailForPageByCondition(tran);
        Map<String,Object> map = new HashMap<>();
            int total = tranService.selectTranForCount();
            map.put("tranList",tranList);
            map.put("total",total);
        return map;
    }

    @RequestMapping("/workbench/transaction/toSavePage.do")
    public String toSavePage(HttpServletRequest request){
        List<User> userList = userService.getAllUser();
        List<DicVlaue> clueStateList = dicValueService.selectDicValueByTypeCode("clueState");
        List<DicVlaue> stageList = dicValueService.selectDicValueByTypeCode("stage");
        List<DicVlaue> returnStateList = dicValueService.selectDicValueByTypeCode("returnState");
        List<DicVlaue> sourceList = dicValueService.selectDicValueByTypeCode("source");
        List<DicVlaue> returnPriorityList = dicValueService.selectDicValueByTypeCode("returnPriority");
        List<DicVlaue> appellationList = dicValueService.selectDicValueByTypeCode("appellation");
        List<DicVlaue> transactionTypeList = dicValueService.selectDicValueByTypeCode("transactionType");

        request.setAttribute("userList",userList);
        request.setAttribute("clueStateList",clueStateList);
        request.setAttribute("stageList",stageList);
        request.setAttribute("returnPriorityList",returnPriorityList);
        request.setAttribute("returnStateList",returnStateList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("appellationList",appellationList);
        request.setAttribute("transactionTypeList",transactionTypeList);
        return "/workbench/transaction/save";
    }

    @ResponseBody
    @RequestMapping("/workbench/transaction/save/selectActivityByName.do")
    public Object selectActivityByName(String name){
        List<Activity> activityList = activityService.selectActivityByName(name);
        return activityList;
    }

    @ResponseBody
    @RequestMapping("/workbench/transaction/sace/selectContactsByName.do")
    public Object selectContactsByName(String fullname){
        List<Contacts> contactsList = contactsService.selectContactsByName(fullname);
        return contactsList;
    }

    @ResponseBody
    @RequestMapping("/workbench/transaction/save/insertTran.do")
    public Object insertTran(Tran tran , HttpSession session){
        User user = (User)session.getAttribute(Contants.SESSION_USER);
        ReturnObject returnObject = new ReturnObject();
        tran.setId(UUID.randomUUID().toString().replaceAll("-",""));
        tran.setCreateTime(DateUtils.formateDateTime(new Date()));
        tran.setCreateBy(user.getId());
        Customer customer = customerService.selectCustomerIdByCustomerName(tran.getCustomerId());
        if (customer==null){
            customer = new Customer();
            customer.setId(UUID.randomUUID().toString().replaceAll("-",""));
            customer.setCreateTime(tran.getCreateTime());
            customer.setCreateBy(user.getId());
            customer.setNextContactTime(tran.getNextContactTime());
            customer.setName(tran.getCustomerId());
            customer.setOwner(user.getId());
            customer.setContactSummary(tran.getContactSummary());
            customer.setDescription(tran.getDescription());
            customerService.insertCustomer(customer);
        }
        tran.setCustomerId(customer.getId());
        TranHistory tranHistory = new TranHistory();
        tranHistory.setId(UUID.randomUUID().toString().replaceAll("-",""));
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setStage(tran.getStage());
        tranHistory.setCreateBy(user.getId());
        tranHistory.setCreateTime(DateUtils.formateDateTime(new Date()));
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setTranId(tran.getId());
        tranHistory.setPossibility(tran.getPossibility());
        try {
            int count = tranService.insertTran(tran);
            int countHistory = tranHistoryService.insertTranHistory(tranHistory);
            if (count>0 && countHistory>0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnObject;
    };

    @ResponseBody
    @RequestMapping("/workbench/transaction/save/getPossibilityByStage.do")
    public Object getPossibilityByStage(String stageValue){
        ResourceBundle resourceBundle = ResourceBundle.getBundle("possibility");
        String possibility = resourceBundle.getString(stageValue);
        return possibility;
    }

    @ResponseBody
    @RequestMapping("/workbench/transaction/save/getCustomerNameByName.do")
    public Object getCustomerNameByName(String name){
        List<Customer> customerList = customerService.selectCustomerByName(name);
        return customerList;
    }

    @RequestMapping("/workbench/transaction/detail.do")
    public String detail(String id,HttpServletRequest request){
        Tran tran = tranService.selectTranDetailByTranId(id);
        List<TranRemark> tranRemarkList = tranRemarkService.selectTranRemarkListByTranId(id);
        List<TranHistory> tranHistoryList = tranHistoryService.selectTranHistoryListByTranId(id);
        List<DicVlaue> stageList = dicValueService.selectDicValueByTypeCode("stage");
        request.setAttribute("tran",tran);
        request.setAttribute("tranRemarkList",tranRemarkList);
        request.setAttribute("tranHistoryList",tranHistoryList);
        request.setAttribute("stageList",stageList);
        return "workbench/transaction/detail";
    }
}
